Js常用方法 发表于 2019-02-05 | 更新于: 2020-02-05 | 分类于 sum , js | 阅读次数: 字数统计: 362 | 阅读时长 ≈ 2 2019.2.5 星期三 16:35 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586/** * Cookie */function getCookie(name){ var reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)") var arr=document.cookie.match(reg) return (arr?arr[2]:null)} // 设置存储数据function setStorage(key, val, cookie, day){ var cookie = key + "=" + encodeURIComponent(val), days = (typeof(day) == 'undefined' || day == 0) ? 3650 : day,// 过期时间,默认10年 exp = new Date(); exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000); cookie = cookie + ";domain=" + cookieDomain + ";path=/;expires=" + exp.toGMTString(); document.cookie = cookie;}// 删除存储数据function delStorage(key, cookie){ if($.trim(key)){ var exp = new Date(), cval = getStorage(key, 'cookie'); exp.setTime(exp.getTime() - 1); if(cval != null){ document.cookie = key + "=" + cval + ";domain=" + cookieDomain + ";path=/;expires=" + exp.toGMTString(); } }}// 清除存储数据function clearStorage(cookie){ var key = document.cookie.match(/[^ =;]+(?=\=)/g); if(key){ for(var i = key.length - 1; i >= 0; i--){ var exp = new Date(), cval = getStorage(key[i], 'cookie'); exp.setTime(exp.getTime() - 1); document.cookie = key[i] + "=" + cval + ";domain=" + cookieDomain + ";path=/;expires=" + exp.toGMTString(); } }}/** * 函数节流/去抖 */function debounce(fn, wait) { var timer = null; return function () { var context = this var args = arguments if (timer) { clearTimeout(timer); timer = null; } timer = setTimeout(function () { fn.apply(context, args) }, wait) }}function throttle(fn, wait) { var previous = 0 var timer = null return function () { var context = this var args = arguments if (!previous) { previous = Date.now() fn.apply(context, args) } else if (previous + wait >= Date.now()) { if (timer) { // console.log(timer) clearTimeout(timer) timer = null } // console.log(timer) timer = setTimeout(function () { // console.log(timer) previous = Date.now() fn.apply(context, args) }, wait) } else { previous = Date.now() fn.apply(context, args) } }} knowledge is no pay,reward is kindness 打赏 微信支付 支付宝